//初步对后端接口进行处理
const express = require("express")
const login = express.Router()
const axios = require("axios")
const jwt = require('jsonwebtoken')

//退出登录
login.get('/login/exit',(req,res)=>{
    req.session.userMessage=undefined
    req.session.token=undefined
    res.status(200).json({
        data:'退出登录成功'
    })
})

//验证码
login.get('/user/code', (req, res) => {
        axios({
            method: 'get',
            url: '/Kaptcha/defaultKaptcha',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },

        }).then(data => {
            // 接收Map集合
            let result = data.data;
            req.session.code = result['code-information']
            console.log(result['code-information']);
            res.status(200).json({
                data: data.data
            })
        }).catch(err => {
            res.status(200).json({
                err: err
            })
        })
    })
    //正常登录功能
login.post('/user/login', (req, res) => {
        let { zhanghao, mima, code } = req.query
        axios({
            method: 'POST',
            url: '/user/login',
            params: {
                account: zhanghao,
                password: mima,
                code: code
            },
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'code-information': req.session.code
            }
        }).then(data => {

            if (data.data.code === 200001) {
                res.status(200).json({
                    data: data.data
                })

            } else {
                console.log(data.data);
                //将token存到session里面
                req.session.token = data.data.data
                let token = jwt.decode(data.data.data)
                console.log(token);
                //将用户信息存到session里面
                req.session.userMessage = {
                    area: token.area,
                    resume: token.resume,
                    sex: token.sex,
                    permission: token.permission,
                    id: token.id,
                    portrait: token.portrait,
                    exp: token.exp,
                    account: token.account,
                    age: token.age,
                    email: token.email,
                    username: token.username
                }
                res.status(200).json({
                    data: data.data,
                    id: req.session.userMessage.id
                })
            }
        }).catch(err => {
            res.status(500).json({
                err: err
            })
        })
    })
    //邮箱登录
login.post('/email/login', (req, res) => {
    console.log(req.session.code);
    let { codes, email, password } = req.query
    axios({
        method: 'POST',
        url: '/user/loginByEmail',
        params: {
            code: codes,
            email: email,
            password: password
        },
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'code-information': req.session.code
        }
    }).then(data => {
        console.log(req.session.code);
        console.log(data.data);
        if (data.data.code === 200001) {
            res.status(200).json({
                data: data.data
            })

        } else {

            //将token存到session里面
            req.session.token = data.data.data
            let token = jwt.decode(data.data.data)
            console.log(token);
            //将用户信息存到session里面
            req.session.userMessage = {
                area: token.area,
                resume: token.resume,
                sex: token.sex,
                permission: token.permission,
                id: token.id,
                portrait: token.portrait,
                exp: token.exp,
                account: token.account,
                age: token.age,
                email: token.email,
                username: token.username
            }
            res.status(200).json({
                data: data.data

            })
        }
    }).catch(err => {
        console.log(err);
        res.status(200).json({
            err: err
        })

    })

})



//邮箱找回密码
login.get('/getCode', (req, res) => {
    let { email } = req.query
    axios({
        method: 'GET',
        url: '/user/getfindPasswordCode',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {
            email: email
        }
    }).then(data => {
        console.log(data.data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        res.status(500).json({
            err: err
        })
    })
})

//检验邮箱邮箱发来的验证码是否与输入框中的验证码一样
login.get('/examinecode', (req, res) => {
    let { email, code } = req.query
    axios({
        method: 'GET',
        url: '/user/isRightEmailCode',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {
            email: email,
            code: code
        }
    }).then(data => {

        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        res.status(500).json({
            err: err
        })

    })
})

//用户设置新密码
login.post('/setnewpassword', (req, res) => {
    let { email, password, repeatpassword } = req.query
    axios({

        method: 'POST',
        url: '/user/findPasswordByEmail',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params: {
            email: email,
            password: password,
            repeatPassword: repeatpassword
        }
    }).then(data => {
        console.log(data.data.data.message);
        console.log(data.data.data.data);
        res.status(200).json({
            data: data.data.data
        })
    }).catch(err => {
        res.status(500).json({
            err: err
        })
    })

})










//注册
login.post('/user/register', (req, res) => {
    let { zhanghao, mima, code } = req.query

    axios({
        method: 'POST',
        url: '/user/insertUser',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'code-information': req.session.code
        },
        params: {
            account: zhanghao,
            password: mima,
            code: code
        }
    }).then(data => {
        console.log(data.data);

        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        console.log(err);
        res.status(500).json({
            err: err
        })
    })
})



//邮箱注册之获取邮箱验证码
login.get('/register/email', (req, res) => {
        let { email: email } = req.query

        axios({
            method: 'GET',
            url: '/user/getEmailCode',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            params: {
                email: email,
            }
        }).then(data => {
            console.log(data.data);
            res.status(200).json({
                data: data.data
            })
        }).catch(err => {
            console.log(err);
            res.status(500).json({
                err: err
            })
        })
    })
    //邮箱注册之验证验证码是否正确
login.post('/rightcode', (req, res) => {
        let { email, code } = req.query
        axios({
            method: 'POST',
            url: '/user/isRegCode',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            params: {
                email: email,
                code: code
            }
        }).then(data => {
            console.log(data.data.data);
            res.status(200).json({
                data: data.data
            })
        }).catch(err => {
            console.log(err);
            res.status(200).json({
                err: err
            })
        })
    })
    //终级邮箱注册
login.post('/finalregister', (req, res) => {
    let { email, password } = req.query
    axios({
        method: 'POST',
        url: '/user/insertUserByEmail',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {
            email: email,
            password: password
        }
    }).then(data => {
        console.log(data.data);
        res.status(200).json({
            data: data.data
        })
    }).catch(err => {
        console.log(err);
        res.status(200).json({
            err: err
        })
    })

})

// login.get('/pictrue/message',(req,res)=>{
//     res.render('../views/common/headNav.art',req.session.userMessage)
// })

module.exports = login